<?php
namespace app\sendmsg\controller;

use app\ApiRest;
use app\card\model\CardBoss;
use app\card\model\CardCount;
use app\card\model\CardExtension;
use app\card\model\CardForward;
use app\card\model\CardMessage;
use app\card\model\CardValue;
use app\card\model\Collection;
use app\card\model\Company;
use app\card\model\UserFollow;
use app\card\model\UserMark;
use app\card\model\UserPhone;
use app\Common\model\LongbingCardCount;
use app\sendmsg\model\CardArticles;
use app\sendmsg\model\CardauthConfig;
use app\sendmsg\model\CardCompany;
use app\sendmsg\model\CardGoods;
use app\sendmsg\model\CardModular;
use app\sendmsg\model\CardShopOrder;
use app\sendmsg\model\CardStatistics;
use app\sendmsg\model\CardTimeline;
use app\sendmsg\model\CardUser;
use app\sendmsg\model\UserInfo;
use app\radar\model\RadarMessage;
use app\radar\model\RadarOrder;
use app\radar\model\RadarUserLabel;
use app\radar\model\RadarUserMark;
use app\shop\model\AdminShopOrderGoods;
use think\App;
use think\facade\Db;

class Service extends apiRest
{
    protected $page;
    protected $list_rows;
    public function __construct ( App $app )
    {
        parent::__construct( $app );
        $this->page = isset( $this->_param[ 'page' ] ) ? $this->_param[ 'page' ] : 1;
        $this->list_rows = isset( $this->_param[ 'list_rows' ] ) ? $this->_param[ 'list_rows' ] : 10;
    }
    public function product_statistics(){
        //统计
        //当前用户所分配的公司
        $whe['uniacid'] = $this->_uniacid;
        $whe['user_id'] = $this->getUserId();
        $whe['status'] = 1;
        $boss = new CardBoss();
        $boss_list = $boss->bosslist($whe);
        if(!$boss_list){
            //查询小程序下所有的公司
            $company = new CardCompany();
            $boss_list = $company->getCompanylist(['uniacid'=>$this->_uniacid,'status'=>1]);
        }
        if(!$boss_list){
            return $this->error('你还没设置公司');
        }
        //查询关联公司的员工
        $user_info = new UserInfo();
        $wheu = [
            ['company_id','in',$boss_list],
            ['status','=',1],
            ['uniacid','=',$this->_uniacid]
        ];
        $user_list = $user_info->getUserlist($wheu);
        $card_count = new CardCount();
        //$stat = new CardStatistics();
//        $where1 = [
//            ['user_id','in',$user_list],
//        ];
        $list = $card_count->getsss($user_list,$this->_uniacid);
        return $this->success($list);
    }

    /**
     * By.jingshuixian
     * 调整内容
     * @return \think\Response
     */
    public function customer_info(){

        $stat = new CardStatistics();
        $user_id = $this->getUserId();
        $where['uniacid'] =  $this->_uniacid;
        $where['user_id'] = $user_id;
        //$where['table'] = 'customer';
        $collect = new Collection();
        //查询七天新增客户
        $wherek = ['intention'=>1,'a.uniacid'=>$this->_uniacid,'a.to_uid'=>$user_id];
        $week_count =$collect->weekToday($wherek);
        //上七天
        $startTime7                   = mktime( 0, 0, 0, date( 'm' ), date( 'd' ) - 13, date( 'Y' ) );
        $endTime7                   = mktime( 0, 0, 0, date( 'm' ), date( 'd' ) - 6, date( 'Y' ) ) - 1;
        $where7 = [
            ['intention','=',1],
            ['a.uniacid','=',$this->_uniacid],
            ['a.to_uid','=',$user_id],
            ['a.update_time','between',"$startTime7,$endTime7"]
        ];
        $bef =$collect->allXian($where7);
//        if ($bef == 0) {
//            $bef = 1;
//        }
//        $proportion = $week_count / $bef * 100;round($proportion, 2)
        $arr['week_customer'] = ['week'=>$week_count,'week_rate'=>$week_count-$bef];
        //客户总数
        $customer_count =$collect->allXian(['intention'=>1,'a.uniacid'=>$this->_uniacid,'a.to_uid'=>$user_id]);
        //今天客户数量
        $todaycoll = $collect->today($wherek);
        $arr['today_customer'] = ['total'=>$customer_count,'today'=>$todaycoll];
        //今天的累计流量量
        $card_count = new CardCount();
        $wherel = ['sign'=>'praise','type'=>2,'uniacid'=>$this->_uniacid,'to_uid'=>$user_id];
        $today_collect = $card_count->gettoday($wherel);
        //累计浏览量
        //$where['table'] = 'CardCount';
        $collect_l =$card_count->getCount($wherel);
        $arr['look_number'] = ['total'=>$collect_l,'today'=>$today_collect];

        //今天线索
        $whereX = [
            ['a.uniacid','=',$this->_uniacid],
            ['a.to_uid','=',$user_id],
            ['intention','=',0],
            ['a.uid','<>',$user_id],
        ];
        $today_visit = $collect->today($whereX);
        //总线索
        $collect_x =$collect->allXian($whereX);
        $arr['visit_number'] = ['total'=>$collect_x+$customer_count,'today'=>$today_visit];

        //转发名片
        //$where['table'] = 'forward';
//        $forward_count = new CardForward();
//        $where['type'] = 0;
//        $where['sign'] = 'forward';
        $wherez = ['sign'=>'praise','type'=>4,'uniacid'=>$this->_uniacid,'to_uid'=>$user_id];
        $zf =$card_count->getCount($wherez);
        //昨日转发名片
        $zf_yesterday =$card_count->getYesterday($wherez);
        $arr['forward_number'] = ['total'=>$zf,'yesterday'=>$zf_yesterday];

        //点赞次数
//        $where['table'] = 'CardCount';
//        $where['type'] = 3;
//        $where['sign'] = 'praise';
        $whered = ['to_uid'=>$user_id,'type'=>3,'sign'=>'praise','uniacid'=>$this->_uniacid];
        $zan = $card_count->getCount($whered);
        //昨日点赞数量
        $card_count = new CardCount();
        $dz_yesterday = $card_count->getYesterday($whered);
        $arr['fabulous_number'] = ['total'=>$zan,'yesterday'=>$dz_yesterday];
        //被保存
        $whereb = ['to_uid'=>$user_id,'type'=>1,'sign'=>'copy','uniacid'=>$this->_uniacid];
        $baocun = $card_count->getCount($whereb);
        //昨日被保存数量
        $card_count = new CardCount();
        $bc_yesterday = $card_count->getYesterday($whereb);
        $arr['preservation_number'] = ['total'=>$baocun,'yesterday'=>$bc_yesterday];

        //销售笔数
        $admin_goods = new CardShopOrder();
        $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
        $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
        $where2 = [['to_uid','=',$user_id]];
        $arr['sales_count'] = $admin_goods->getGoosSale($where2);
        $where2[] = [['create_time','between',"$beginYesterday,$endYesterday"]];
        $arr['yesterday_sales_count'] = $admin_goods->getGoosSale($where2);

        $users = new CardUser();
        $user_in = $users->getUser(['id'=>$user_id]);
        if($user_in['is_boss'] != 1){
            $arr['high'] = [];
        }else{
            //当前用户所分配的公司
            $whe['uniacid'] = $this->_uniacid;
            $whe['user_id'] = $user_id;
            $whe['status'] = 1;
            $boss = new CardBoss();
            $boss_list = $boss->bosslist($whe);
            if(!$boss_list){
                //查询小程序下所有的公司
                $company = new CardCompany();
                $boss_list = $company->getCompanylist(['uniacid'=>$this->_uniacid,'status'=>1]);
            }
            if(!$boss_list){
                $arr['high'] = [];
            }
            //查询关联公司的员工
            $user_info = new UserInfo();
            $wheu = [
                ['company_id','in',$boss_list],
                ['status','=',1],
                ['uniacid','=',$this->_uniacid]
            ];
            $user_id = $user_info->getUserlist($wheu);
            $frist = $user_info->getzhlist(
                [['a.fans_id','in',$user_id],
                    ['a.uniacid','=',$this->_uniacid],
                    ['b.is_staff','=',1]],1, 1,0,1);

            $arr['high'] = $frist['data'][0]['name'];
        }
        return $this->success($arr);
    }
    public function comprehensive(){
        $param = input('param.');
        $type = $param['type'];
        $desc = $param['desc'];
        $user_id = $this->getUserId();
        if ($type ==4) {
            if(!empty($param['time'])){
                if ($param['time'] == 7) {
                    $time = 7;
                } elseif ($param['time'] == 15) {
                    $time = 15;
                } elseif ($param['time'] == 30) {
                    $time = 30;
                }else{
                    $time = 1;
                }
            }else{
                $time = 1;
            }
        }
        if(!empty($time)){
            $start=mktime(0,0,0,date('m'),date('d')-$time,date('Y'));
            $end=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
            $where[] = [
                [ 'a.create_time', 'BETWEEN', [$start, $end] ],
            ];
        }
        //当前用户所分配的公司
        $whe['uniacid'] = $this->_uniacid;
        $whe['user_id'] = $user_id;
        $whe['status'] = 1;
        $boss = new CardBoss();
        $boss_list = $boss->bosslist($whe);
        if(!$boss_list){
            //查询小程序下所有的公司
            $company = new CardCompany();
            $boss_list = $company->getCompanylist(['uniacid'=>$this->_uniacid,'status'=>1]);
        }
        if(!$boss_list){
            $arr['high'] = [];
        }
        //查询关联公司的员工
        $user_info = new UserInfo();
        $wheu = [
            ['company_id','in',$boss_list],
            ['status','=',1],
            ['uniacid','=',$this->_uniacid]
        ];
        $user_id = $user_info->getUserlist($wheu);
        $where[] =[
            ['a.fans_id','in',$user_id],
            ['a.uniacid','=',$this->_uniacid],
            ['b.is_staff','=',1],['a.is_staff','=',1]
        ];
        $frist = $user_info->getzhlist(
            $where,$this->page, $this->list_rows,$type,$desc);
        $frist[ 'data' ] = formatDefaultImage( $frist[ 'data' ], 'avatar', 'avatar', $this->defaultImage );
        return $this->success($frist);

    }
    public function total_info(){
        $user = $this->getUserId();
        $users = new CardUser();
        $user_in = $users->getUser(['id'=>$user]);
        if($user_in['is_boss'] != 1){
            return $this->error('用户还未成为boss');
        }
        $collect = new Collection();
        $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
        $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;

        $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
        $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;

        $beginbeforeYesterday=mktime(0,0,0,date('m'),date('d')-2,date('Y'));
        $endbeforeYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'))-1;
        //当前用户所分配的公司
        $whe['uniacid'] = $this->_uniacid;
        $whe['user_id'] = $user;
        $whe['status'] = 1;
        $boss = new CardBoss();
        $boss_list = $boss->bosslist($whe);
        if(!$boss_list){
            //查询小程序下所有的公司
            $company = new CardCompany();
            $boss_list = $company->getCompanylist(['uniacid'=>$this->_uniacid,'status'=>1]);
        }
        if(!$boss_list){
            return $this->error('你还没设置公司');
        }
        //查询关联公司的员工
        $user_info = new UserInfo();
        $wheu = [
            ['company_id','in',$boss_list],
            ['status','=',1],
            ['uniacid','=',$this->_uniacid]
        ];
        $user_id = $user_info->getUserlist($wheu);
        $stat = new CardStatistics();
        //累计线索
        $whes[] = [
            ['a.uid','not in',$user_id],['a.to_uid','in',$user_id],['a.uniacid','=',$this->_uniacid]
        ];
        $cumulative_clues = $collect->todayUid($whes);
        //日增长线索
        $todayKh = $collect->today($whes);
        //昨天线索
        $yesterdaytodayKh = $collect->Yesterday($whes);
        //前天的线索
        $whes[] =[
            ['a.create_time', 'BETWEEN', [ $beginbeforeYesterday, $endbeforeYesterday ]]
        ];
        $beforKh = $collect->todayUid($whes);
        //昨天对比前天新增数
        $contrastKh =$yesterdaytodayKh-$beforKh;

        //线索池
        $whes1[] =[
            ['intention','=',0],['a.uid','not in',$user_id],['a.to_uid','in',$user_id],['a.uniacid','=',$this->_uniacid]
        ];
        $xs = $collect->todayUid($whes1);
        //今天的线索池
        $whes1[1] =[[ 'a.create_time', 'BETWEEN', [ $beginToday, $endToday ]]];
        $todayxs = $collect->todayUid($whes1);
        //昨天线索池
        $whes1[1] =[[ 'a.create_time', 'BETWEEN', [ $beginYesterday, $endYesterday ]]];
        $yesterdayxs = $collect->todayUid($whes1);
        //前天线索池
        $whes1[1] =[[ 'a.create_time', 'BETWEEN', [ $beginbeforeYesterday, $endbeforeYesterday ]]];
        $beforexs = $collect->todayUid($whes1);
        //昨天对比前天新增数
        $contrastxs =$yesterdayxs-$beforexs;
        $whez[] =[
            ['intention','=',1],['a.uid','not in',$user_id],
            ['a.to_uid','in',$user_id],
            ['a.uniacid','=',$this->_uniacid]
        ];

//        //累计客户数量
        $customer = $collect->todayUid($whez);

        //日增长客户
        $today = $collect->today($whez);
        //日线索增长
        $addTodayxs = $todayxs-$today;
        //昨天客户数
        $yesterdaykh = $collect->Yesterday($whez);
        //前天的客户
        $whez[] = [
            [ 'a.update_time', 'BETWEEN', [ $beginbeforeYesterday, $endbeforeYesterday ]]
        ];
        $beforkh = $collect->todayUid($whez);
        //昨天对比前天新增数
        $contrastkh =$yesterdaykh-$beforkh;


        //累计访问量
        $card_count = new CardCount();
//        $visit_number = $stat->getCustomerCount([['user_id','in',$user_id],
//            ['sign','=','praise'],
//            ['type','=',2],['uniacid','=',$this->_uniacid]]);
        //今天的访问量
        $wheres[] = [
            ['to_uid','in',$user_id],
            ['sign','=','praise'],
            ['type','=',2],
            ['uniacid','=',$this->_uniacid]
        ];
        $visit_number = $card_count->getCount($wheres);
        $wheres[1] = [
            [ 'create_time', 'BETWEEN', [ $beginToday, $endToday ] ],
        ];
        $todayVisitNumber = $card_count->getCount($wheres);
        //昨天的访问量
        $wheres[1] = [
            [ 'create_time', 'BETWEEN', [ $beginYesterday, $endYesterday ] ],
        ];
        $yesterdayVisit = $card_count->getCount($wheres);
        //前天的访问量
        $wheres[1] = [
            [ 'create_time', 'BETWEEN', [ $beginbeforeYesterday, $endbeforeYesterday] ],
        ];
        $beforVisit = $card_count->getCount($wheres);
        //昨天对比前天新增数
        $contrastVisit =$yesterdayVisit-$beforVisit;

        //咨询信息
        //消息
    $whereMsg[] = [
        ['message_type','=','text'],
        ['deleted','=',0],
        ['uniacid','=',$this->_uniacid],
        ['target_id','in',$user_id]
    ];
       $card_message = new CardMessage();
       //累计消息
        $message = $card_message->getCount($whereMsg);
        //今天消息
        $whereMsg[1] = [
            [ 'create_time', 'BETWEEN', [ $beginToday, $endToday ] ],
        ];
        $today_message = $card_message->getCount($whereMsg);
        //昨天的消息
        $whereMsg[1] = [
            [ 'create_time', 'BETWEEN', [ $beginYesterday, $endYesterday ] ],
        ];
        $Yesterday_message = $card_message->getCount($whereMsg);
        //前天的消息
        $whereMsg[1] = [
            [ 'create_time', 'BETWEEN', [ $beginbeforeYesterday, $endbeforeYesterday ] ],
        ];
        $before_message = $card_message->getCount($whereMsg);

        $zx = $card_count->zxInfo($user_id);
        $zx = $zx+$message;
       $where1 =  ['create_time','BETWEEN',[ $beginToday, $endToday ]];
        $zxtoday = $card_count->zxInfo($user_id,$where1);
        $zxtoday = $zxtoday+$today_message;
        //昨天的咨询
        $where1 =  ['create_time','BETWEEN',[ $beginYesterday, $endYesterday ]];
        $zxYesterday = $card_count->zxInfo($user_id,$where1);
        $zxYesterday = $zxYesterday+$Yesterday_message;
        //前天的咨询
        $where1 =  ['create_time','BETWEEN',[ $beginbeforeYesterday, $endbeforeYesterday ]];
        $zxbefore = $card_count->zxInfo($user_id,$where1);
        $zxbefore = $zxbefore+$before_message;
        //昨天对比前天新增数
        $contrastzx =$zxYesterday-$zxbefore;

        //跟进客户次数UserFollow
        $user_mark = new UserFollow();
        $where3[] = [
            ['staff_id','in',$user_id],
            ['status','=',1],
            //['uniacid','=',$this->_uniacid]
        ];
        $mark =  $user_mark->getCount($where3);
        $where3[1] =  [ 'create_time', 'BETWEEN', [ $beginToday, $endToday ] ];
        $today_mark = $user_mark->getCount($where3);
        //昨天的跟进客户
        $where3[1] =  [ 'create_time', 'BETWEEN', [ $beginYesterday, $endYesterday ] ];
        $Yesterday_mark = $user_mark->getCount($where3);
        //前天的跟进客户
        $where3[1] = [
            [ 'create_time', 'BETWEEN', [ $beginbeforeYesterday, $endbeforeYesterday ] ],
        ];
        $before_mark = $user_mark->getCount($where3);
        $contrastzmark =$Yesterday_mark-$before_mark;
        //被转发次数
 //       $forward = new CardForward();
//        $wheref[] =[
//            ['user_id','in',$user_id],
//            ['uniacid','=',$this->_uniacid],
//            ['table','=','forward']
//        ];

        $where4[] = [
            ['to_uid','in',$user_id],
            ['type','=',4],
            ['sign','=','praise'],
            ['uniacid','=',$this->_uniacid],
        ];
//        $forward_count = $stat->getCustomerCount($where4);
        $forward_count = $card_count->getCount($where4);
        $today_forward_count =$card_count->gettoday($where4);
        //昨天转发
        $Yesterday_forward_count =$card_count->getYesterday($where4);
        //前天转发
        $where4[] = [
            [ 'create_time', 'BETWEEN', [$beginbeforeYesterday, $endbeforeYesterday] ],
        ];
        $before_forward_count =$card_count->getCount($where4);
        $contrastzforward =$Yesterday_forward_count-$before_forward_count;
        //累计被点赞次数
        $whered[] = [
            ['user_id','in',$user_id],
            ['type','=',3],
            ['sign','=','praise'],
            ['uniacid','=',$this->_uniacid],
        ];
//        $dz = $stat->getCustomerCount($whered);
        $where5[] = [
            ['to_uid','in',$user_id],
            ['type','=',3],
            ['sign','=','praise'],
            ['uniacid','=',$this->_uniacid]
        ];
        $dz = $card_count->getCount($where5);
        $where5[1] = [
            [ 'create_time', 'BETWEEN', [ $beginToday, $endToday ] ],
        ];
        $today_dz = $card_count->getCount($where5);
        //昨天点赞
        $where5[1] = [
            [ 'create_time', 'BETWEEN', [$beginYesterday, $endYesterday] ],
        ];
        $Yesterday_dz = $card_count->getCount($where5);
        //前天点赞
        $where5[1] = [
            [ 'create_time', 'BETWEEN', [$beginbeforeYesterday, $endbeforeYesterday] ],
        ];
        $before_dz = $card_count->getCount($where5);
        $contrastzdz =$Yesterday_dz-$before_dz;
        //累计被保存次数
        $where6[] = [
            ['to_uid','in',$user_id],
            ['type','=',1],
            ['sign','=','copy'],
            ['uniacid','=',$this->_uniacid]
        ];
        $bc = $card_count->getCount($where6);
        $where6[1] = [
            [ 'create_time', 'BETWEEN', [ $beginToday, $endToday ] ],
        ];
        $today_bc = $card_count->getCount($where6);
        //昨天保存
        $where6[1] = [
            [ 'create_time', 'BETWEEN', [$beginYesterday, $endYesterday] ],
        ];
        $Yesterday_bc = $card_count->getCount($where6);
        //前天保存
        $where6[1] = [
            [ 'create_time', 'BETWEEN', [$beginbeforeYesterday, $endbeforeYesterday] ],
        ];
        $before_bc = $card_count->getCount($where6);
        $contrastzbc =$Yesterday_bc-$before_bc;
        //商城订单笔数和销售金额
        $admin_goods = new CardShopOrder();
        $where7[]= [
            ['company_id','in',$boss_list],
            ['to_uid','in',$user_id],
            ['uniacid','=',$this->_uniacid]
        ];
        $sales_count = $admin_goods->getGoosSale($where7);
        //昨天的商品销售
        $where7[1] = [
            [ 'create_time', 'BETWEEN', [$beginYesterday, $endYesterday] ],
        ];
        $Yesterday_sales_count = $admin_goods->getGoosSale($where7);
        //数据
        $arr = [
          'count'=>[
              [
                  [
                      'name'=>'累计线索(人)',
                      'number'=>$cumulative_clues,
                      'rate'=>$todayKh,
                  ],
                  [
                      'name'=>'线索池(人)',
                      'number'=>$xs,
                      // 'text'=>$title,
                      'rate'=>$addTodayxs,
                      // 'up'=>$up
                  ],
                  [
                      'name'=>'累计客户(人)',
                      'number'=>$customer,
                      'rate'=>$today,
                  ],
                  [
                      'name'=>'累计访问量(次)',
                      'number'=>$visit_number,
                      'rate'=>$todayVisitNumber,
                  ],
                  [
                      'name'=>'累计咨询(次)',
                      'number'=>$zx,
                      'rate'=>$zxtoday,
                  ],
                  [
                      'name'=>'累计跟进客户(次)',
                      'number'=>$mark,
                      'rate'=>$today_mark,
                  ],
                  [
                      'name'=>'累计被转发(次)',
                      'number'=>$forward_count,
                      'rate'=>$today_forward_count,
                  ],
                  [
                      'name'=>'累计被点赞(次)',
                      'number'=>$dz,
                      'rate'=>$today_dz,
                  ],
                  [
                      'name'=>'累计被保存(次)',
                      'number'=>$bc,
                      'rate'=>$today_bc,
                  ]
              ],
              [
                  [
                      'name'=>'新增线索(人)',
                      'number'=>$yesterdaytodayKh,
                      'rate'=>$contrastKh,
                  ],
                  [
                      'name'=>'变动线索池(人)',
                      'number'=>$yesterdayxs,
                      'rate'=>$contrastxs,
                  ],
                  [
                      'name'=>'新增客户(人)',
                      'number'=>$yesterdaykh,
                      'rate'=>$contrastkh,
                  ],
                  [
                      'name'=>'新增访问量(次)',
                      'number'=>$yesterdayVisit,
                      'rate'=>$contrastVisit,
                  ],
                  [
                      'name'=>'新增咨询(次)',
                      'number'=>$zxYesterday,
                      'rate'=>$contrastzx,
                  ],
                  [
                      'name'=>'新增跟进客户(次)',
                      'number'=>$Yesterday_mark,
                      'rate'=>$contrastzmark,
                  ],
                  [
                      'name'=>'新增被转发(次)',
                      'number'=>$Yesterday_forward_count,
                      'rate'=>$contrastzforward,
                  ],
                  [
                      'name'=>'新增被点赞(次)',
                      'number'=>$Yesterday_dz,
                      'rate'=>$contrastzdz,
                  ],
                  [
                      'name'=>'新增被保存(次)',
                      'number'=>$Yesterday_bc,
                      'rate'=>$contrastzbc,
                  ]
              ],
          ],
            'goods_sale'=>[
                $sales_count,$Yesterday_sales_count
            ]
        ];
        $title1 = '日增涨';
        $title2 = '日减少';
        $title3 = '持平 -';
         foreach ($arr['count'][0] as $key=>$value){

             if($value['rate']>0){
                 $arr['count'][0][$key]['text'] = $title1;
                 $arr['count'][0][$key]['up'] =1;
             }elseif ($value['rate']==0){
                 $arr['count'][0][$key]['up'] =-1;
                 $arr['count'][0][$key]['text'] = $title3;
             }else{
                 $arr['count'][0][$key]['up'] =0;
                 $arr['count'][0][$key]['text'] = $title2;
             }
             $arr['count'][0][$key]['rate'] = abs($value['rate']);
         }
        foreach ($arr['count'][1] as $k=>$val){
            if($val['rate']>0){
                $arr['count'][1][$k]['text'] = $title1;
                $arr['count'][1][$k]['up'] =1;
            }elseif ($val['rate']==0){
                $arr['count'][1][$k]['up'] =-1;
                $arr['count'][1][$k]['text'] = $title3;
            }else{
                $arr['count'][1][$k]['up'] =0;
                $arr['count'][1][$k]['text'] = $title2;
            }
            $arr['count'][1][$k]['rate'] = abs($val['rate']);
        }
        //查询小程序时间
        $create_time = $users->getUser(['uniacid'=>$this->_uniacid]);
        $timediff = time() - $create_time['create_time'];
        $arr['day'] = intval($timediff / 86400);
        $arr['time'] = '今天'.date('H:i',time());
        return $this->success($arr);
    }
    public function yesterday_clue(){
        $user = $this->getUserId();
        $users = new CardUser();
        $user_in = $users->getUser(['id'=>$user]);
        if($user_in['is_boss'] != 1){
            return $this->error('用户还未成为boss');
        }
        //当前用户所分配的公司
        $whe['uniacid'] = $this->_uniacid;
        $whe['user_id'] = $user;
        $whe['status'] = 1;
        $boss = new CardBoss();
        $boss_list = $boss->bosslist($whe);
        if(!$boss_list){
            //查询小程序下所有的公司
            $company = new CardCompany();
            $boss_list = $company->getCompanylist(['uniacid'=>$this->_uniacid,'status'=>1]);
        }
        if(!$boss_list){
            return $this->error('你还没设置公司');
        }
        //查询关联公司的员工
        $user_info = new UserInfo();
        $wheu = [
            ['company_id','in',$boss_list],
            ['status','=',1],
            ['uniacid','=',$this->_uniacid]
        ];
        $user_id = $user_info->getUserlist($wheu);
        $collect = new Collection();
        $where[] =[
            ['b.is_staff','=',1],
            ['intention','=',0],
            ['a.to_uid','in',$user_id],
            ['a.uniacid','=',$this->_uniacid]
        ];
        $list = $collect->getYesterdaylist($where);
        $card_count = new CardCount();
        //客户兴趣
        $interest = $card_count->doPageBossInterest(['to_uid','in',$user_id],$this->_uniacid);
        //客户活跃度
        $active = $card_count->active($user_id,$this->_uniacid,1);
        return $this->success(['data'=>$list,'interest'=>$interest,'active'=>$active]);
    }
    public function team_enter(){
        $param = input('param.');
        $type = $param['type'];
        $desc = $param['desc'];

        if ($type ==2) {
            if(!empty($param['time'])){
                if ($param['time'] == 7) {
                    $time = 7;
                } elseif ($param['time'] == 15) {
                    $time = 15;
                } elseif ($param['time'] == 30) {
                    $time = 30;
                }else{
                    $time = 1;
                }
            }else{

                $time = 1;
            }
        }

        $user = $this->getUserId();
        $users = new CardUser();
        $user_in = $users->getUser(['id'=>$user]);
        if($user_in['is_boss'] != 1){
            return $this->error('用户还未成为boss');
        }
        //当前用户所分配的公司
        $whe['uniacid'] = $this->_uniacid;
        $whe['user_id'] = $user;
        $whe['status'] = 1;
        $boss = new CardBoss();
        $boss_list = $boss->bosslist($whe);
        if(!$boss_list){
            //查询小程序下所有的公司
            $company = new CardCompany();
            $boss_list = $company->getCompanylist(['uniacid'=>$this->_uniacid,'status'=>1]);
        }
        if(!$boss_list){
            return $this->error('你还没设置公司');
        }
        //查询关联公司的员工
        $user_info = new UserInfo();
        $wheu = [
            ['company_id','in',$boss_list],
            ['status','=',1],
            ['uniacid','=',$this->_uniacid]
        ];
        $user_id = $user_info->getUserlist($wheu);
        $collect = new Collection();
        if($type == 1||$type ==2){
            $where[] = [
                ['intention','=',0],
            ];
        }
        if($type == 3){
            $where[] = [
                ['intention','=',1],
            ];
        }
        $where[] =[
            ['a.to_uid','in',$user_id],
            ['a.uniacid','=',$this->_uniacid],
            ['b.is_staff','=',1],
        ];
        if(!empty($time)){
            $start=mktime(0,0,0,date('m'),date('d')-$time,date('Y'));
            $end=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
            $where[] = [
                [ 'a.create_time', 'BETWEEN', [$start, $end] ],
            ];
        }
        $list = $collect->getlistAll($where,$this->page, $this->list_rows,$type,$desc);
        $list[ 'data' ] = formatDefaultImage( $list[ 'data' ], 'avatar', 'avatar', $this->defaultImage );
        return $this->success($list);
    }
    protected function chaeckBossUser(){
        $user = $this->getUserId();
        $users = new CardUser();
        $user_in = $users->getUser(['id'=>$user]);
        if($user_in['is_boss'] != 1){
            return -1;
        }
        //当前用户所分配的公司
        $whe['uniacid'] = $this->_uniacid;
        $whe['user_id'] = $user;
        $whe['status'] = 1;
        $boss = new CardBoss();
        $boss_list = $boss->bosslist($whe);
        if(!$boss_list){
            //查询小程序下所有的公司
            $company = new CardCompany();
            $boss_list = $company->getCompanylist(['uniacid'=>$this->_uniacid,'status'=>1]);
        }
        if(!$boss_list){
            return -2;
        }
        //查询关联公司的员工
        $user_info = new UserInfo();
        $wheu = [
            ['company_id','in',$boss_list],
            ['status','=',1],
            ['uniacid','=',$this->_uniacid]
        ];
        $user_id = $user_info->getUserlist($wheu);
        return $user_id;
    }
    public function staff_info(){
        //获取值
        $user_list = $this->chaeckBossUser();
        if($user_list ==-1){
            return $this->error('用户还未成为boss');
        }
        if($user_list ==-2){
            return  $this->error('你还没设置公司');
        }
        $card_value = new CardValue();
        $sss = $card_value->bossAi($user_list,$this->_uniacid);
        $param = input('param.');
        $verify = ['staff_id' => 'required'];
        $params = lbGetParamVerify($param, $verify);
        $collect = new Collection();
        $where['b.fans_id'] =  $params['staff_id'];
        $info = $collect->staffInfo($where,$this->page, $this->list_rows);
        $ass = [];
        if(!empty($info['ability'])){
            foreach ($info['ability']['data'] as $k=>$v){
                if($v['number'] == $sss['max_number']||$v['number'] ==0){
                    $ass[] = $v['number'];
                }else{
                    $ass[] = ceil($v['number']/$sss['max_data'][$k]*$sss['max_number']);
                }
            }
        }
        $info['ability']['rate'] = $ass;
        $info['ability']['max_rate'] = $sss['max_number'];
        return $this->success($info);
    }
    public function more_customer(){
        $param = input('param.');
        $verify = ['staff_id' => 'required'];
        $params = lbGetParamVerify($param, $verify);
        $follow_info = UserFollow::alias( 'a' )
            ->join( 'longbing_card_user b', 'a.user_id = b.id')
            ->field( [ 'content', 'a.create_time','user_id','b.nickName as name' ] )
            ->where( [
                    [ 'a.staff_id', '=',$params['staff_id'] ] ]
            )->order( 'a.id', 'desc' )
            ->paginate( [ 'list_rows' => $this->list_rows, 'page' => $this->page ])->toArray();
        $follow_info['data'] = lbHandelRadarDate( $follow_info['data'], 'create_time' );
        foreach ($follow_info['data'] as $index => $item )
        {
            $follow_info['data'][ $index ][ 'create_time' ] = date( 'Y-m-d H:i', $item[ 'create_time' ] );
        }
        return $this->success($follow_info);
    }
    public function more_interaction(){
        $param = input('param.');
        $verify = ['staff_id' => 'required'];
        $params = lbGetParamVerify($param, $verify);
        $ardar = new CardCount();
        $radar_count = $ardar->radarList([ [ 'a.to_uid', '=', $params['staff_id']  ],[ 'a.user_id', '<>', $params['staff_id']  ] ], $this->page,$this->list_rows);
        $radar_count['data'] = lbHandelRadarDate( $radar_count['data'], 'create_time' );
        if($radar_count['data']){
            foreach ($radar_count['data'] as $k => $v )
            {
                $radar_count['data'][ $k ][ 'create_time' ] = date( 'Y-m-d H:i', $v[ 'create_time' ] );
            }
        }
        return $this->success($radar_count);
    }
    public function test(){
        //当前用户所分配的公司

//        //$user_info = new UserInfo();
//        $company = new Company();
//        $where = [
//            ['b.is_staff','=',1]
//        ];
//        $data = $company->alias('a')
//            ->join( 'longbing_card_user_info b', 'a.id = b.company_id')
//            ->join( 'longbing_card_user c', 'b.fans_id = c.id')
//            ->field('b.fans_id as to_uid,a.uniacid,b.company_id')
//            ->where($where)
//            ->select()->toArray();
//        if($data){
//            foreach ($data as $k=>$v){
//                $card_value = new CardValue();
//                $rest = $card_value->bossGetAiValue($v['to_uid'],$v['uniacid']);
//                $data[$k]['total_info'] = $rest['data'];
//                $data[$k]['total'] =  $rest['max'];
//            }
//            if($data){
//                $last_names = array_column($data,'total');
//                array_multisort($last_names,SORT_DESC,$data);
//            }
//        }

    }
    public function total_details(){
        $param = input('param.');
        $verify = ['staff_id' => 'required','type'=>0];
        $params = lbGetParamVerify($param, $verify);
        $userId = $params['staff_id'];
        $type = $params['type'];
            $where   = [ [ 'a.to_uid', '=',$userId ],[ 'a.uid', '<>', $userId ] ];
            $data = Collection::alias( 'a' )
                ->field( [ 'a.uid','a.rate','c.nickName as name', 'c.avatarUrl as avatar' ]
                )
                ->where( $where )
                ->join( 'longbing_card_user c', 'a.uid = c.id' )
                ->order( 'a.rate desc , a.uid asc')
                ->group( 'a.uid, a.to_uid' )
                ->paginate( [ 'list_rows' => $this->list_rows, 'page' => $this->page ] )->each(function ($value)use ($userId,$type){
                    if($type ==0){
                        //  通话数量
                        $telCount = CardCount::where( [ [ 'user_id', '=', $value[ 'uid' ] ], [ 'to_uid', '=', $userId ],
                                [ 'sign', '=', 'copy' ], [ 'type', 'in', [ 2, 3, 11 ] ] ]
                        )->count();
                        if($telCount>0){
                            $telCount = '联系'.$telCount.'次';
                        }else{
                            $telCount = '未联系过';
                        }
                        $value['text'] = $telCount;
                    }
                   if($type ==1){
                       //  聊天数量
                       $map1         = [ [ 'user_id', '=', $value[ 'uid' ] ], [ 'target_id', '=', $userId ] ];
                       $map2         = [ [ 'target_id', '=', $value[ 'uid' ] ], [ 'user_id', '=', $userId ] ];
                       $messageCount = RadarMessage::whereOr( [ $map1, $map2 ] )
                           ->count();
                       if($messageCount>0){
                           $messageCount = '咨询'.$messageCount.'次';
                       }else{
                           $messageCount = '未咨询过';
                       }
                       $value[ 'text' ] = $messageCount;
                   }
                   if($type ==2){
                       //跟进次数
                       $mark = new UserMark();
                       $value['text'] = $mark->getMarkStatus([[ 'user_id', '=', $value[ 'uid' ] ], [ 'staff_id', '=', $userId ],[ 'status', '=',1 ] ]);
                   }
                    return $value;
                })
                ->toArray();

          $data[ 'data' ] = formatDefaultImage( $data[ 'data' ], 'avatar', 'avatar', $this->defaultImage );
        return $this->success($data);
    }

}